/**
 * 日志管理初始化
 */
var SdcBusiOptLog = {
    id: "OptLogTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdcBusiOptLog.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '日志类型', field: 'logType', align: 'left', valign: 'middle', sortable: true,width:120},
        {title: '日志名称', field: 'logName', align: 'left', valign: 'middle', sortable: true,width:120},
        {title: 'IP地址', field: 'ipaddr', align: 'center', valign: 'middle', sortable: true,width:120},
       /* {title: '操作系统', field: 'os', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '浏览器', field: 'browser', align: 'center', valign: 'middle', sortable: true,width:150},*/
        {title: '医生用户', visible: true, field: 'userName', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '就诊号', visible: true, field: 'episodeId', align: 'center', valign: 'middle', sortable: true,width:100},
        {title: '参数', visible: true,field: 'params', align: 'left', valign: 'middle', sortable: true,width:240},
        {title: '时间', field: 'createDate', align: 'center', valign: 'middle', sortable: true,width:200},
        // {title: '具体消息', field: 'message', align: 'left', valign: 'middle'}
    ];
};

/**
 * 检查是否选中
 */
SdcBusiOptLog.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if(selected.length == 0){
        Sys.info("请先选中表格中的某一记录！");
        return false;
    }else{
        SdcBusiOptLog.seItem = selected[0];
        return true;
    }
};

/**
 * 查看日志详情
 */
SdcBusiOptLog.detail = function () {
    if (this.check()) {
        Sys.ajax(Sys.backendPath + "/sdc/sdcbusioperationlog/detail/" + this.seItem.id, function (data) {
            Sys.openHtml("日志详情", data.regularMessage, ['800px', '600px']);
        });
    }
};


/**
 * 清空日志
 */
SdcBusiOptLog.delLog = function () {
    Sys.confirm("是否清空所有日志?",function(){
        Sys.ajax(Sys.backendPath + "/sdc/sdcbusioperationlog/delete",function(res){
        	Sys.success("清空日志成功！");
        });
        SdcBusiOptLog.table.refresh();
    });
}

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
SdcBusiOptLog.formParams = function() {
    var queryData = {};

    queryData['logName'] = $("#logName").val();
    queryData['episodeId'] = $("#episodeId").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['userName'] = $("#userName").val();
    queryData['logType'] = $("input[name='logType']").val();
    queryData['hospital'] = $("input[name='hospital']").val();
    queryData['depart'] = $("input[name='depart']").val();
    queryData['medicalGroup'] = $("input[name='medicalGroup']").val();
    queryData['qcentity'] = $("input[name='qcentity']").val();

    return queryData;
}

/**
 * 查询日志列表
 */
SdcBusiOptLog.search = function () {
    SdcBusiOptLog.table.refresh({query: SdcBusiOptLog.formParams()});
};

/**
 * //根据父级机构 查询子机构
 */
SdcBusiOptLog.officeChange = function(that){
    var ajax = new $ax("/sds/common/deptListByHospId", function(data){
        $('#depart').combobox({
            data:data,
            valueField:'value',
            textField:'text',
            defaultFilter: 6,
            onSelect:SdcBusiOptLog.medicalGroupChange,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        layer.msg("查询失败!" + data.responseJSON.message + "!",{icon: 2});
    });
    ajax.set({"hospID":that.value,"id":"depart"});
    ajax.start();
}

/**
 * 切换科室，查询下属医疗组
 */
SdcBusiOptLog.medicalGroupChange = function(that){
    var ajax = new $ax(Sys.backendPath + "/sds/common/groupListByDeptId", function(data){
        $('#medicalGroup').combobox({
            data:data,
            valueField:'value',
            textField:'text',
            defaultFilter: 6,
            onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
                $(this).combobox("setValue", "");
            }
        });
    },function(data){
        layer.msg("查询失败!" + data.responseJSON.message + "!",{icon: 2});
    });
    ajax.set({"deptId":that.value?that.value:that,"id":"medicalGroup"});
    ajax.start();
}

$(function () {
    //院区
    $('#hospital').combobox({
        data:$.parseJSON($("#officeMap").val()),
        valueField:'value',
        textField:'text',
        onSelect:SdcBusiOptLog.officeChange,
        onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
            if ($("#hospitalbak").val() != null) {
                $(this).combobox("setValue", $("#hospitalbak").val());
            }
        }
    });

    //科室组
    $('#depart').combobox({
        data:$.parseJSON($("#departMap").val()),
        valueField:'value',
        textField:'text',
        defaultFilter:6,
        onSelect:SdcBusiOptLog.medicalGroupChange,
        onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
            if ($("#departbak").val() != null) {
                $(this).combobox("setValue", $("#departbak").val());
                SdcBusiOptLog.medicalGroupChange($("#departbak").val())
            }
        }
    });

    //医疗组
    $('#medicalGroup').combobox({
        // data:$.parseJSON($("#departMap").val()),
        valueField:'value',
        textField:'text',
        defaultFilter:6,
        onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
            if ($("#medicalGroupbak").val() != null) {
                $(this).combobox("setValue", $("#medicalGroupbak").val());
            }
        }
    });
    //病种
    $('#qcentity').combobox({
        data:$.parseJSON($("#entityMap").val()),
        valueField:'value',
        textField:'text',
        defaultFilter:6,
        onLoadSuccess: function () { //加载完成后,val[0]写死设置选中第一项
            if ($("#qcentitybak").val() != null) {
                $(this).combobox("setValue", $("#qcentitybak").val());
            }
        }
    });

    var defaultColunms = SdcBusiOptLog.initColumn();
    var table = new BSTable(SdcBusiOptLog.id, Sys.backendPath + "/sdc/sdcbusioperationlog/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(SdcBusiOptLog.formParams());
    table.setSort("createDate");
    table.setPageSize(20);
    SdcBusiOptLog.table = table.init();
});
